from bge import logic
from bge import events
import random

def CollStacja():
	cont = logic.getCurrentController()
	simon = cont.owner

	if simon['lastDest'] != simon['dest']:
		print('dd')



def Player():
	
	cont = logic.getCurrentController()
	player = cont.owner
	scene = logic.getCurrentScene()
			
	move = cont.actuators['move']
	track = cont.actuators['track']
	speed = [0,6,0]

	colStacja = cont.sensors['colStacja']
	colPraca = cont.sensors['colPraca']
	colRozrywka = cont.sensors['colRozrywka']
	colSzkola = cont.sensors['colSzkola']

	attrPower = cont.sensors['attrPower']


	
	
	def Init():
		
		global lastPosition
		if not 'init' in player:
			player['init'] = 1
	
	def happyAmount(money):
		price = 1
		#max40% pieniedzy an sczescie
		amount = round(money*0.4, 0) * price
		cost = amount * price

		result = [int(amount), int(cost)]
		return result

	def getAmount(value, procent, price):
		price = 1		
		amount = round(value*procent, 0) * price
		cost = amount * price
		result = [int(amount), int(cost)]
		return result

	def powerAmount(money, currentPower):
		price = 1
		
		limitPower = 1000		
		
		amount = int(round(money/price, 0))
		
		if (amount+currentPower) >= limitPower:
			amount = limitPower - currentPower


		cost = amount * price

		
		result = [int(amount), int(cost)]

		#print("Kupno paliwa: ", result)
		return result

	def getTarget():
		#Szkola, Stacja, Rozrywka, Praca
		objects = ["Szkola", "Rozrywka", "Praca"]

		needed = getNeeded()

		if needed == 'power':
			player['dest'] = 'Stacja'
		elif needed == 'money':
			player['dest'] = 'Praca'
		elif needed == 'knowledge':
			player['dest'] = 'Szkola'
		elif needed == 'happiness':
			player['dest'] = 'Rozrywka'
		else:
			return player['dest']

		#print("Pobieranie celu: ", player['dest'])
		return player['dest']
   
	def movePlayer():
		player['power'] -= 1
		

	def getNeeded():
		#print("Czy potrzeba?")
		if player['needed'] != "":
			
			return player['lastNeeded']


		power = player['power']
		knowledge = player['knowledge']
		happiness = player['happiness']
		money = player['money']

		neededList = ['power', 'knowledge', 'happiness', 'money']
		
		lastNeeded = player['lastNeeded']

		if power < 375 :			
			if player['money'] < 100:
				player['needed'] = 'money'
			else:
				player['needed'] = 'power'
		elif money < 300:
			if lastNeeded != 'money':
				player['needed'] = 'money'
			else:
				if player['money'] > 50:
					player['needed'] = 'knowledge'
		elif happiness < 150 :
			if lastNeeded != 'happiness':		
				player['needed'] = 'happiness'
			elif money > 350:
				player['needed'] = 'knowledge'
			else:
				player['needed'] = 'money'
		
		elif player['knowledge'] < 1000:
			if lastNeeded != 'knowledge':
				player['needed'] = 'knowledge'
			else:
				player['needed'] = 'happiness'
		elif money < 200:
			player['needed'] = 'money'
		else:			
			if money > 350:
				player['needed'] = 'knowledge'
			elif lastNeeded != 'happiness':
				player['needed'] = 'happiness'
			else:
				player['needed'] = 'money'

		player['lastNeeded'] = player['needed']
		
		print("Pobierz potrzebe", player['needed'])

		return player['needed']

	def colisionAction(collisionObj):
		player['needed'] = ""
		#print("CZYSZCZENIE POTRZEBY")
		track.object = getTarget()
		#print("Pobieranie celu: ", track.object)
		player['lastDest'] = collisionObj.hitObject.name
		#speed[1] = 0
		
	def getColor(value):
		r = 0
		g = 1
		b = 0

		g = (value/1000) - (0.1)
		r = (1-(value/1000)) + (0.1)


		return (r,g,b,1)

	def Update(): 	
		
		#print(attrPower.value)

		player.color = getColor(player['power'])
		turbo = False
		#Only start
		if track.object is None:
			track.object = getTarget()
			print('START')
		

		if colStacja.positive and player['dest'] != player['lastDest'] and player['dest'] == 'Stacja':
			print('Kolizja: STACJA')
			if player['power'] < 1000:
				turbo = True
				amountHappy = getAmount(player['happiness'], 0.2, 1)
				_buyPower = powerAmount(player['money'], player['power'])			
				player['power'] += _buyPower[0]			
				player['money'] -= _buyPower[1]			
				player['happiness'] -= amountHappy[0]							
			colisionAction(colStacja)

		if colPraca.positive and player['dest'] != player['lastDest'] and player['dest'] == 'Praca':
			print('Kolizja: PRACA')
			stawka = 150
			
			amountHappy = getAmount(player['happiness'], 0.3, 1)
			amountPower = getAmount(player['power'], 0.2, 1)

			wiedza = player['knowledge']
			player['money'] += stawka*wiedza
			player['power'] -= amountPower[0]
			player['happiness'] -= amountHappy[1]
			colisionAction(colPraca)

		if colRozrywka.positive and player['dest'] != player['lastDest'] and player['dest'] == 'Rozrywka':
			print('Kolizja: ROZRYWKA')
			amount = happyAmount(player['money'])
			amountPower = getAmount(player['power'], 0.1, 1)
			print("BUY HAPPY: ", amount)
			player['happiness'] += amount[0]
			player['power'] -= amountPower[1]
			player['money'] -= amount[1]
			colisionAction(colRozrywka)

		if colSzkola.positive and player['dest'] != player['lastDest'] and player['dest'] == 'Szkola':
			print('Kolizja: SZKOLA')
			amountHappy = getAmount(player['happiness'], 0.1, 1)
			player['knowledge'] += 1
			player['money'] -= 10
			player['happiness'] -= amountHappy[0]
			colisionAction(colSzkola)
		
		
		#if player.getDistanceTo(track.object) <= 2.5:			
		track.object = getTarget()
			

		if player['power'] > 0:			     
			cont.activate(track)				
			movePlayer()
			if player['power'] < 350:
				speed[1] = 3.9
			if player['power'] < 150:
				player['happiness'] -= 0.1
				speed[1] = 3.9
			if player['power'] > 760:
				speed[1] = 5
			if turbo:
				speed[1] = 10
		else:
			speed[1] = 0
			player.endObject()


		#------------- MOVE--------------------
		move.linV = speed
		move.useLocalLinV = 1
		cont.activate(move) 	
	
	Init()	
	Update()
	
